import pymysql
from db.model import DB_Model
#个人银行账户管理系统


def showUserAccountInfo(userId):
    global model
    user=model.find(userId)
    print("=" * 20, "用户信息查询结果", "=" * 21)
    print("|{0:<10}|{1:<10}|{2:<10}|".format("用户编号","姓名",  "账户余额"))
    print("|{0:<10}|{1:<10}|{2:<10}|".format( user["id"], user["name"],user["balance"]))
    print("")
    return user

def drawMoney(user,amount):
    global model
    if int(user["balance"])< int(amount):
        print("余额不足！")
        return
    else:
        new_balance=str(int(user["balance"])-int(amount))
        affect=model.update({'id':user['id'],'balance':new_balance})
        if affect:
            print("取款成功")
            print("您的当前余额是：", new_balance)
        else:
            print("取款失败，请重新操作")



def saveMoney(user,amount):
    global model
    new_balance = str(int(user["balance"]) + int(amount))
    print("存款成功")
    print("您的当前余额是：", new_balance)
    model.update({'id':user['id'],'balance':new_balance})

#获取数据库连接模型
model=DB_Model("account")
#获取所有用户信息，以用于登入
userInfoList = model.findAll()
logingout=False
while True:
    #输出登入页面
    print("="*20,"银行个人账户查询系统","="*20)
    print("")
    print("{0:<25}|{1:<25}".format("1，登入", "2，退出系统"))
    yourInput = input("请输入你的操作选项:")
    if yourInput == '1':
        print("-"*20,"请输入登入信息","-"*20)
        isLogin=False
        name =input("请输入用户名:")
        password =input("请输入密码:")
        for user in userInfoList:
            if user["name"] == name and user["password"] == password:
                print("")
                print("登入成功", "欢迎您", name)
                isLogin = True
                userid=user["id"]
                while True:
                    print("=" * 20, "银行个人账户管理系统", "=" * 20)
                    print("|{0:<25}|{1:<25}".format("1，查询", "2，取钱"))
                    print("|{0:<25}|{1:<25}".format("3，存钱", "4，退出"))
                    print("=" * 20, "选择你的操作序列号", "=" * 21)
                    yourChose = input("请输入你的操作选项:")
                    if yourChose == '1':
                        print("")
                        showUserAccountInfo(userid)
                    elif yourChose == '2':
                        user = model.find(userid)
                        print("您的当前余额是：", user['balance'])
                        amount = input("请输入你要取款金额：")
                        drawMoney(user, amount)
                    elif yourChose == '3':
                        user = model.find(userid)
                        print("您的当前余额是：", user['balance'])
                        amount = input("请输入你要存款金额：")
                        saveMoney(user, amount)
                    elif yourChose == '4':
                        print("=" * 20, "再见", "=" * 33)
                        print("")
                        print("")
                        logingout=True
                        break
                    elif yourChose == '':
                        print("请输入操作序列号")
                        print("")
                    else:
                        print("没有这个选项，重新选择")
                        print("")
            if logingout:
                    break


        if not isLogin:
            print("")
            print("用户信息错误，重新输入")
            print("")

    else:
        print("=" * 20, "再见", "=" * 33)
        print("")
        print("")
        break








